<?php
require_once 'init.inc';

$title = 'Отправка заказа';

$basket = new Basket;
$orderId = 0;

if ($par->exist('name') && !$basket->isEmpty())
{
	$order = Orders::create(
		$par->get('name'),
		$par->get('email'),
		$par->get('phone'),
		$par->get('address'),
		$par->get('comments')
	);
	
	$order->addBasket($basket);
	
	$email = 'lodkipalatki@yandex.ru';
	$subject = "Заказ #" . $order->id();
	
	$text = 
		"Заказ #" . $order->id() . "\n" .
		"Дата " . date('d.m.Y H:i',$order->createdAt()) . "\n\n" .
		"Заказанные товары\n" . 
			$basket->__toString() . "\n" .
		"Данные о заказчике\n".
			"ФИО: " . $par->get('name')."\n" .
			"Телефон: " . $par->get('phone') . "\n" .
			"Email: " . $par->get('email') . "\n" .
			"Адрес:\n" . $par->get('address') . "\n" .
			"Комментарии:\n" . $par->get('comments') . "\n";
			
	mail($email, $subject, $text, 'Content-Type:text/plain; charset="utf8"');

	$orderId = $order->id();
	$basket->purge();
}

$products = $basket->toArray();

assignTemplateVars('title','products','basket','orderId');
display('buy');
?>